<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\SendEmail;
use App\Http\Requests\Api\CaptchasRequest;

class CaptchasController extends Controller
{
    public function store(CaptchasRequest $request)
    {
        //生成验证码 缓存10分钟 并验证
        $key = $request->action.'-'.str_random(15);
        $expiredAt = now()->addMinutes(20);
        $email = $request->email;
        $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);
        \Cache::put($key, ['email'=>$email,'code' => $code], $expiredAt);
        $result = [
            'email_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
        ];
        dispatch(new SendEmail($code, $email, $request->action))->onQueue('emails');
        return $this->response->array($result)->setStatusCode(201);
    }

    public function getVersion()
    {
        $result = [
            'isConfirme' => 0, //未审核,
            'loUrl' => 'https://www.baidu.com',
            'tabHome' => '首页',
            'tabSquare' => '广场',
            'tabMessage' => '消息',
            'tabInfo' => '个人',
            'tabSquare' => '广场',
            'homeNavItem' => '即时新闻',
            'squareNavItem' => '话题广场',
            'messageNavItem' => '我的评论',
            'settingNavItem' => '个人设置',
        ];
        return $this->response->array($result)->setStatusCode(201);
    }
}
